import ClipboardJS from 'clipboard';
import TipUtil from "@/utils/message.js";

const clipboard = {
    instance: null,
    /**
     * 创建对象
     */
    __instance: function (selector, text) {
        this.instance = new ClipboardJS(selector, {
            text: function () {
                return text;
            }
        });
        this.instance.on('success', function (instance) {
            TipUtil.success('复制成功');
            // 复制成功之后，销毁当前实力，避免下一次重复触发
            clipboard.instance.destroy();
        });
        this.instance.on('error', function (e) {
            TipUtil.error('复制失败');
        });
        return this.instance;
    },
    copyContent: function (selector, text) {
        this.__instance(selector, text);
    }
};

export default clipboard;